package com.dylan.springcloud.feign.controller;

import com.dylan.springcloud.feign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * Created by Dylan on 2018/10/12.
 */
@RestController
public class HelloFeignController {

    @Autowired
    private HelloFeignService helloFeignService;

    @GetMapping(value = "/search/github")
    public String searchGithubRepoByStr(@RequestParam("str") String queryStr) {
        return helloFeignService.searchRepo(queryStr);
    }

    @GetMapping(value = "/search/github/gzip")
    public ResponseEntity<byte[]> searchGithubRepoByStrGzip(@RequestParam("str") String queryStr) {
        ResponseEntity<byte[]> responseEntity = helloFeignService.searchRepoGzip(queryStr);
        if (responseEntity != null && responseEntity.getBody() != null) {
            System.out.println(responseEntity.getHeaders());
        }
        return responseEntity;
    }

    @GetMapping(value = "/search/github/gzip2")
    public String searchGithubRepoByStrGzip2(@RequestParam("str") String queryStr) {
        ResponseEntity<byte[]> responseEntity = helloFeignService.searchRepoGzip(queryStr);
        if (responseEntity != null && responseEntity.getBody() != null) {
            System.out.println(responseEntity.getHeaders());
            // responseEntity.getBody()为压缩后数据，不能进行解码，这样会乱码
            String result = new String(responseEntity.getBody(), StandardCharsets.UTF_8);
            return result;
        }
        return "";
    }
}
